﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clock
{
    public partial class UcClock : UserControl
    {
        public UcClock()
        {
            InitializeComponent();
        }

        private void UcClock_Load(object sender, EventArgs e)
        {

        }

        private void UcClock_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            g.Clear(Color.AliceBlue);
            g.SmoothingMode = SmoothingMode.HighQuality;
 
            //坐标系转移和旋转
            var center = new Point(this.Width / 3, this.Height / 2);
            g.TranslateTransform(center.X, center.Y);
            //画圆
            var pen = new Pen(new SolidBrush(Color.Blue));
            pen.Width = 3.0F;
            g.DrawEllipse(pen, -120, -120, 250, 250);
            g.FillEllipse(Brushes.Black, -4, -4, 8, 8);

            //画刻度
            g.DrawLine(pen, new PointF(5, -120), new PointF(5, -110));
            g.DrawLine(pen, new PointF(5, 130), new PointF(5, 120));
            g.DrawLine(pen, new PointF(120, 0), new PointF(130, 0));
            g.DrawLine(pen, new PointF(-120, 0), new PointF(-110, 0));
            Font font = new Font("仿宋", 10F);
            Brush brush = Brushes.Black;
            g.DrawString("12", font, brush, new PointF(-3, -110));
            g.DrawString("6", font, brush, new PointF(0, 108));
            g.DrawString("3", font, brush, new PointF(110, -5));
            g.DrawString("9", font, brush, new PointF(-110, -5));

            //当前秒数
            var seconds = System.DateTime.Now.Second;
            g.RotateTransform(seconds * 6);
            var pen1 = new Pen(new SolidBrush(Color.Green));
            var p1 = new Point(0, 0);
            var p2 = new Point(0, -100);
            g.DrawLine(pen1, p1, p2);
            g.RotateTransform(seconds * 6 * (-1));

            //当前分钟
            var minutes = System.DateTime.Now.Minute;
            g.RotateTransform(minutes *6);
            var pen2 = new Pen(new SolidBrush(Color.Red));
            pen2.Width = 1.5F;
            var p3 = new Point(0, -70);
            g.DrawLine(pen2, p1, p3);
            g.RotateTransform(minutes * 6 * (-1));

            //当前小时
            var hours = System.DateTime.Now.Hour;
            g.RotateTransform((hours * 3600+minutes*60)/120);
            var pen3 = new Pen(new SolidBrush(Color.Black));
            pen3.Width = 2.0F;
            var p4 = new Point(0, -50);
            g.DrawLine(pen3, p1, p4);
            g.RotateTransform((hours * 3600 +minutes*60)/120*(-1));


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //定时重绘控件
            this.Invalidate();
        }
    }
}
